//
//  RootView.m
//  豆瓣2
//
//  Created by dllo on 15/12/19.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "RootView.h"


@implementation RootView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createSubView];
        
}
   
    return self;
}

- (void)createSubView{
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.scrollView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(4*414, 0);
    for (int i = 1; i < 5; i++) {
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(414*(i-1), 0, 414, self.frame.size.height)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image%d.png",i]];
        [self.scrollView addSubview:imageView];
        if (i==4) {
            UIButton *button = [[UIButton alloc]init];
            button.frame = CGRectMake((414-100)/2, self.frame.size.height-300, 200, 100);
            [button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
            [button setTitle:@"欢迎进入下一页" forState:UIControlStateNormal];
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            imageView.userInteractionEnabled = YES;
            [imageView addSubview:button];
        }
        
    }
    // 按页滑动
    self.scrollView.pagingEnabled = YES;
    
    UIPageControl *pageC = [[UIPageControl alloc]initWithFrame:CGRectMake((414-200)/2, self.frame.size.height-50, 200, 50)];
    pageC.numberOfPages = 4;
    [self addSubview:pageC];
    pageC.currentPage = 0;
    
    [pageC addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    

}

- (void)pageAction:(UIPageControl *)page{
    
    [self.scrollView setContentOffset:CGPointMake(page.currentPage *414, 0) animated:YES];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    UIPageControl *pageControl = [self viewWithTag:1000];
    pageControl.currentPage = scrollView.contentOffset.x/414;
}

- (void)buttonAction:(UIButton *)bnt{
    
    self.userInteractionEnabled = NO;
    [UIView animateWithDuration:0.3 animations:^{
        
        self.alpha = 0.0;
        
    }completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        [self.myDelegate show];

    }];
    
    
}


@end
